const app = getApp();
Page({
  data: {
    phoneNumber: '15888888888',
    phoneError: false,
    identifyingCode: "1234",
  },
  onPhoneInput(e) {
    const { phoneError } = this.data;
    const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
    if (phoneError === isPhoneNumber) {
      this.setData({
        phoneError: !isPhoneNumber,
      });
    }
  },
  //发送验证码
  onPhoneCode() {
    wx.request({
      url: 'https://xuexiwuapi.jomoit.com/visitor-api/apiUser/getSmsCode',
      method: 'POST', 
      header: {
        "Content-Type": "application/json"
      },
      data: {
        username: this.data.phoneNumber
      },
      success: function(res) {
        const { data } = res;
        const { code, msg } = data;
        console.log(code, msg)
      }
    })
  },
  onLogin() {
    wx.request({
      url: 'https://xuexiwuapi.jomoit.com/visitor-api/apiUser/wxLogin',
      method: 'POST', 
      header: {
        "Content-Type": "application/json"
      },
      data: {
        username: this.data.phoneNumber,
        code: this.data.identifyingCode,
      },
      success: function(res) {
        const { data } = res;
        const { code, msg } = data;
        const { flag, token } = data.data;
        app.globalData.token = token;
        app.globalData.flag = flag;
        if(flag === "0") {
          //访客模式
          wx.navigateTo({
            url: '/pages/enter/enter',
          })
        }else {
          //审核模式
          wx.navigateTo({
            url: '/pages/listVisitedOrder/listVisitedOrder',
          })
        }
      }
    })
  }
})
